<?php
namespace app\index\controller;

use app\common\model;

class menu extends Base
{
    public $tableName = "Menu";

    public function index()
    {
        $menu = model("Menu");
        $menu_list = $menu->order("p_id asc,sort desc")->select()->toArray();
        $data["menu_tree"] = array_to_tree($menu_list);
        $this->assign($data);
        return view();
    }

    public function add()
    {
        $model = model('Menu');
        if (request()->instance()->isPost()) {
            $post = request()->instance()->param();
            $data["title"] = $post["title"];
            $data["url"] = strtolower($post["url"]);
            $data["sort"] = $post["sort"];
            $data["p_id"] = intval($post["p_id"]);
            $data["type"] = intval($post["type"]);
            $menuModel = model("Menu");
            $result = $menuModel->operateMenu($data);
            if (empty($result)) {
                $this->error("操作失败");

            }
            $this->success("操作成功", url("Index/Menu/index"));
        }
        $menu = model("Menu");
        $id = request()->get("id");

        $data["info"] = $menu->get($id);
        $data["menu_type"] = model("CodeMap")->get_value("menu_type");
        $menu_list = $menu->order("p_id asc,sort desc")->select()->toArray();
        $menu_list = array_to_select($menu_list);

        $data["menu_list"] = $menu_list;
        $this->assign($data);
        return view();

    }

    public function edit()
    {
        $model = model('Menu');
        if (request()->instance()->isPost()) {
            $post = request()->instance()->param();
            $data["title"] = $post["title"];
            $data["url"] = strtolower($post["url"]);
            $data["sort"] = $post["sort"];
            $data["p_id"] = intval($post["p_id"]);
            $data["type"] = intval($post["type"]);
            $menuModel = model("Menu");
            $map["id"] = $post["id"];

            $result = $menuModel->operateMenu($data, $map);
            if (empty($result)) {
                $this->error("操作失败");

            }
            $this->success("操作成功", url("Index/Menu/index"));
        }
        $menu = model("Menu");
        $id = input("id");

        $data["info"] = $menu->get($id);

        $menu_list = $menu->order("p_id")->select()->toArray();
        $menu_list = array_to_select($menu_list);
        $data["menu_type"] = model("CodeMap")->get_value("menu_type");
        $data["menu_list"] = $menu_list;
        $this->assign($data);
        return view();
    }

    public function del()
    {
        $id = input("id");
        if (empty($id)) {
            $this->error("删除失败");
        }
        $menu = model("Menu");
        $result = $menu->delMenu($id);
        if ($result) {
            $this->success("删除成功");
        }
        $this->error("删除失败");
    }

    public function test()
    {
        $menu = model("Menu");
        $menu_list = $menu->all()->toArray();
        $data["menu_tree"] = array_to_tree($menu_list);
        $this->assign($data);
        return view();
    }

    /**
     *
     */
    public function access()
    {

        $menu = model("Menu");
        $role_id = input("role_id");
        $menu = model("Menu");
   
    
        $menu_list = $menu->order("p_id asc,level asc")->select()->toArray();
        $data["role"] = model("Role")->where(["id" => $role_id])->find();
        $menu_type = model("CodeMap")->get_value("menu_type");
        $role_menu=model("Role")->menus($role_id);
        $data["menu_type"] = array_column($menu_type, "m_name", "m_value");    
        $data["menu_tree"] = array_to_tree($menu_list);      
        $data["menu_ids"] = array_column( $role_menu,"id");
      
        $data["role_id"] = $role_id;
        $this->assign($data);
        return view();
    }

    /**
     *
     */
    public function save()
    {
        $post = request()->instance()->param();
        $role_id = $post["role_id"];

        $menu_ids = $post["menu_ids"];

        if (empty($role_id)) {
            $this->error("用户角色不能错误");
        }
        if (empty($menu_ids)) {
            $this->error("请选择权限");
        }
        $accessData = [];
        if (is_array($menu_ids)) {
            foreach ($menu_ids as $id) {
                $data["role_id"] = $role_id;
                $data["menu_id"] = $id;
                $accessData[] = $data;
            }
        } else {
            $data["role_id"] = $role_id;
            $data["menu_id"] = $menu_ids;
            $accessData[] = $data;
        }

        model("RoleAccess")->where(["role_id" => $role_id])->delete();
        $result = model("RoleAccess")->saveAll($accessData);
        if (empty($result)) {
            $this->error("权限设置失败");
        }
        $this->success("权限设置成功");

    }

}
